package one.stand.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import one.stand.aop.AdminController;
import one.stand.aop.ApiController;
import one.stand.entity.excel.BusinessExcelEntity;
import one.stand.entity.excel.CompanyExcelEntity;
import one.stand.enums.ResultEnum;
import one.stand.model.MenuModel;
import one.stand.model.ResultModel;
import one.stand.request.*;
import one.stand.response.CompanyResponse;
import one.stand.response.CrmCustomerAnalysisRespnse;
import one.stand.response.VersionResponse;
import one.stand.util.ExcelUtil;
import one.stand.util.ExportDataDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author: ZRH
 * @CreateDate: 2019/4/17 9:45
 */
@RestController
@RequestMapping("adminApi/company")
@Api(description = "总后台公司管理")
public class CompanyController extends BaseController {

    @ApiOperation(value = "审核公司")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "companyId", value = "公司id", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "auditState", value = "状态", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "auditRemark", value = "备注", paramType = "String"),
    })
    @PostMapping("auditCompany")
    @AdminController(tourist = true)
    public ResultModel auditCompany(CompanyRequest request) {
        return adminFegin.auditCompany(request);
    }

    @ApiOperation(value = "公司购买名片服务数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "companyId", value = "公司id", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "purchaseNum", value = "购买数量", paramType = "Integer"),
            @ApiImplicitParam(name = "payNum", value = "支付金额", paramType = "Integer"),
            @ApiImplicitParam(name = "endTime", value = "到期时间", required = true, paramType = "String"),
            @ApiImplicitParam(name = "isAgent", value = "公司是否代理:0不是代理商  1是", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "companyType", value = "1、服务（普通），2、源码（高级）", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "agentMoney", value = "公司代理金额", paramType = "Integer"),
    })
    @PostMapping("addCompanyService")
    @AdminController(tourist = true, isTourist = true)
    public ResultModel addCompanyService(CompanyServiceRequest request,@RequestAttribute TokenRequest tokenRequest) {
        request.setId(tokenRequest.getId());
        return adminFegin.addCompanyService(request);
    }

    @ApiOperation(value = "查询企业列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "enterpriseType", value = "状态:1普通企业  2授权企业  3到期企业", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "companyName", value = "条件筛选", paramType = "String"),
    })
    @PostMapping("selectCompanyList")
    public ResultModel<Map<String, Object>> selectCompanyList(CompanyRequest request) {
        return adminFegin.selectCompanyList(request);
    }

    @ApiOperation(value = "导出用户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "enterpriseType", value = "状态:1普通企业  2授权企业  3到期企业", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "companyName", value = "条件筛选", paramType = "String"),
    })
    @PostMapping("selectCompanyListExport")
    @AdminController(tourist = true)
    public void selectCompanyListExport(CompanyRequest request, HttpServletResponse response) {
        ResultModel<ExportDataDTO<CompanyExcelEntity>> resultModel= adminFegin.selectCompanyListExport(request);
        if(resultModel.getCode().equals(ResultEnum.SUCCESS.getCode())){
            ExportDataDTO file=resultModel.getData();
            try{
                ExcelUtil.exportExcel(file, CompanyExcelEntity.class,response);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    @ApiOperation(value = "查询企业信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "companyId", value = "公司id", required = true, paramType = "Integer"),
    })
    @PostMapping("getAdminCompanyInfo")
    public ResultModel<CompanyResponse> getAdminCompanyInfo(CompanyRequest request) {
        return adminFegin.getAdminCompanyInfo(request);
    }

    @ApiOperation(value = "查询代理商企业下公司的信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "companyId", value = "公司id", required = true, paramType = "Integer"),
    })
    @PostMapping("selectAgentCompanyList")
    public ResultModel<Map<String, Object>> selectAgentCompanyList(CompanyRequest request) {
        return adminFegin.selectAgentCompanyList(request);
    }

    @ApiOperation(value = "设置企业代理权限和代理价格")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "companyId", value = "公司id", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "agentMoney", value = "公司代理金额", paramType = "Integer"),
            @ApiImplicitParam(name = "isAgent", value = "公司是否代理0不是代理商  1是", paramType = "Integer"),
    })
    @PostMapping("configureAgen")
    @AdminController(tourist = true, isTourist = true)
    public ResultModel<Boolean> configureAgen(CompanyRequest request,@RequestAttribute TokenRequest tokenRequest) {
        request.setId(tokenRequest.getId());
        return adminFegin.configureAgen(request);
    }

    @ApiOperation(value = "查询总后台管理员列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "companyId", value = "公司id", paramType = "Integer"),
            @ApiImplicitParam(name = "loginName", value = "登录名称", paramType = "String"),
    })
    @PostMapping("selectAdminList")
    @AdminController(tourist = true)
    public ResultModel<Map<String, Object>> selectAdminList(AdminRequest request) {
        return adminFegin.selectAdminList(request);
    }

    @ApiOperation(value = "新增总/修改后台管理员")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "adminId", value = "管理员id",  paramType = "Integer"),
            @ApiImplicitParam(name = "companyId", value = "公司id", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "loginName", value = "登录名", required = true, paramType = "String"),
            @ApiImplicitParam(name = "passWord", value = "密码", required = true, paramType = "String"),
            @ApiImplicitParam(name = "roleIds", value = "菜单权限id(id数组)", required = true, paramType = "Integer[]"),
            @ApiImplicitParam(name = "name", value = "姓名", paramType = "String"),
            @ApiImplicitParam(name = "sex", value = "性别", paramType = "Integer"),
            @ApiImplicitParam(name = "position", value = "职位", paramType = "String"),
    })
    @PostMapping("saveAdmin")
    @AdminController(tourist = true)
    public ResultModel<Integer> saveAdmin(AdminRequest request) {
        return adminFegin.saveAdmin(request);
    }

    @ApiOperation(value = "设置总后台管理员权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "adminId", value = "管理员id", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "roleIds", value = "菜单权限id(id数组)", required = true, paramType = "Integer[]"),
    })
    @PostMapping("inserAdminRole")
    @AdminController(tourist = true)
    public ResultModel<Boolean> inserAdminRole(AdminRequest request) {
        return adminFegin.inserAdminRole(request);
    }

    @PostMapping("delAdmin")
    @ApiOperation(value = "删除管理员")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "adminId", value = "管理员id", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "isDel", value = "0有效  1无效", required = true, paramType = "Integer"),
    })
    @AdminController(tourist = true)
    public ResultModel<Boolean> delAdmin(AdminRequest request) {
        return adminFegin.delAdmin(request);
    }

    @ApiOperation(value = "查询管理员权限列表")
    @PostMapping("selectMenuList")
    @AdminController(tourist = true)
    public ResultModel<List<MenuModel>> selectMenuList(AdminRequest request) {
        return adminFegin.selectMenuList(request);
    }

    @ApiOperation(value = "为普通企业设置代理商")
    @PostMapping("updateAgentCompanyId")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "companyId", value = "公司id", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "agentCompanyId", value = "代理商id", required = true, paramType = "Integer"),
    })
    @AdminController(tourist = true)
    public ResultModel<Boolean> updateAgentCompanyId(CompanyRequest request) {
        return adminFegin.updateAgentCompanyId(request);
    }
}
